package UI
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * 垂直滚动条
	 * 滚动滑块滚动时，抛ScrollEvent事件
	 * @author jianhong
	 */ 
	public class ScrollBarV extends Sprite
	{
		
		private	var _scrollBtn:Button = null;
		private	var _upArrowBtn:Button = null;
		private var _downArrowBtn:Button = null;
		
		private	var _scrollBtnDown:Boolean = false;
		private	var _recordMousePt:Point = null;
		
		private	var _scrollView:IScrollView = null;
		
		public function ScrollBarV(width:int, height:int, 
								  scrollBarImg:Bitmap, 
								  scrollBgImg:Bitmap,
								  upArrowImg:Bitmap,
								  downArrowImg:Bitmap)
		{
			super();
			
			this._upArrowBtn = new Button(13, 13, upArrowImg);
			this._downArrowBtn = new Button(13,13,downArrowImg);
			this._downArrowBtn.y = height - this._downArrowBtn.height;
			
			var scrollBg:Button = new Button(13, height-this._upArrowBtn.height-this._downArrowBtn.height, scrollBgImg);
			scrollBg.y = this._upArrowBtn.y+this._upArrowBtn.height;
			
			this._scrollBtn = new Button(13, 80, scrollBarImg);
			this._scrollBtn.y = this._upArrowBtn.y+this._upArrowBtn.height;
			
			this.addChild(scrollBg);
			this.addChild(this._scrollBtn);
			this.addChild(this._upArrowBtn);
			this.addChild(this._downArrowBtn);
			
			this.width = width;
			this.height = height;
			
			this._scrollBtn.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this._upArrowBtn.addEventListener(MouseEvent.CLICK, onClickUpArrow);
			this._downArrowBtn.addEventListener(MouseEvent.CLICK, onClickDownArrow);
		}

		public	function	set scrollView(view:IScrollView):void{
			this._scrollView = view;
			var per:Number = view.viewHeight/view.documentHeight;
			var h:int = this.height - this._upArrowBtn.height - this._downArrowBtn.height;
			this._scrollBtn.height = h*per;
			if (this._scrollBtn.height < 20){
				this._scrollBtn.height = 20;
			}
			this._scrollView.addEventListener(ScrollViewAlterEvent.type, onScrollViewAlter);
		}
		
		public	function	onScrollViewAlter(evt:ScrollViewAlterEvent):void{
			var oldPer:Number = this.pos/this.maxPos;
			if (this.pos == 0){
				oldPer = 0;
			}
			var per:Number = evt.viewHeight/evt.documentHeight;
			var h:int = this.height - this._upArrowBtn.height - this._downArrowBtn.height;
			this._scrollBtn.height = h*per;
			if (this._scrollBtn.height < 20){
				this._scrollBtn.height = 20;
			}
			this._scrollBtn.y = this.maxPos*oldPer+this._upArrowBtn.height;
		}
		
		public	function	onClickUpArrow(evt:MouseEvent):void{
			this.scroll(-20);
		}
		
		public	function	onClickDownArrow(evt:MouseEvent):void{
			this.scroll(20);
		}
		
		public	function	onMouseDown(evt:MouseEvent):void{
			this._scrollBtnDown = true;
			this._recordMousePt = this.localToGlobal(new Point(this.mouseX, this.mouseY));
			
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}

		public	function	onMouseUp(evt:MouseEvent):void{
			this._scrollBtnDown = false;
			
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		public	function	onMouseMove(evt:MouseEvent):void{
			if (this._scrollBtnDown == true){
				var pt:Point = this.localToGlobal(new Point(this.mouseX, this.mouseY));
				var dis:int = pt.y - this._recordMousePt.y;
				this.scroll(dis);
				this._recordMousePt = pt;
			}
		}
		
		public	function	scroll(cy:int):void{
			var y:int = this._scrollBtn.y;
			y += cy;
			if (y < this._upArrowBtn.height){
				y = this._upArrowBtn.height;
			}
			else if(y > this.height - this._downArrowBtn.height-this._scrollBtn.height){
				y = this.height - this._downArrowBtn.height-this._scrollBtn.height;
			}
			
			if (y != this._scrollBtn.y){
				var posDiff:int = y - this._scrollBtn.y;
				this._scrollBtn.y = y;
				var evt:ScrollEvent = new ScrollEvent();
				evt.pos = this.pos;
				evt.maxPos = this.maxPos;
				this.dispatchEvent(evt);
				
				var per:Number = posDiff/this.maxPos;
				var scrollCY:Number = (this._scrollView.documentHeight-this._scrollView.viewHeight)*per;
				var tmpValue:Number = Math.abs(scrollCY);
				tmpValue = Math.ceil(tmpValue);
				scrollCY = scrollCY < 0 ? -tmpValue:tmpValue;
				this._scrollView.scrollV(scrollCY);
			}
		}
		
		public	function	get pos():int{
			var curPos:int = this._scrollBtn. y - this._upArrowBtn.height;
			return curPos;
		}
		
		public	function	get maxPos():int{
			var posMax:int = this.height - this._downArrowBtn.height-this._scrollBtn.height - this._upArrowBtn.height;
			return posMax;
		}
	}
}